package main

import (
	"context"
	"fmt"
	"time"
)

func main() {
	d := time.Now().Add(50 * time.Millisecond)
	ctx, cancel := context.WithDeadline(context.Background(), d)
	defer cancel()
	select {
	case <-ctx.Done():
		fmt.Println(ctx.Err()) //Err方法会返回当前Context结束的原因，它只会在Done返回的Channel被关闭时才会返回非空的值
		//打印context deadline exceeded
	case <-time.After(time.Second):
		fmt.Println("overslept")
	}
}
